package com.das.mechanic_base.widget;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.bumptech.glide.Glide;
import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3StringUtils;
import com.kproduce.roundcorners.RoundImageView;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by chensi on 2018/3/9.
 */

public class X3ShowImageDialog extends Dialog {

    private Point mScreenPoint = new Point();
    private Activity mRootActivity = null;

    private RoundImageView iv_image;
    private RoundImageView iv_bg;
    private Button btn_yes;

    /**
     * 没有按钮的提示dialog
     */
    public X3ShowImageDialog(Activity arg) {
        super(arg, R.style.pointDialog);
        mRootActivity = arg;
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(R.layout.x3_show_image_dialog);
        initDialogWindow();
        initView();
    }

    public void showImage(String url) {
//        Glide.with(mRootActivity).load(StringUtils.getImageUrl(url)).apply(RequestOptions.bitmapTransform(new BlurTransformation(25,3)).centerCrop()).into(iv_bg);
        Glide.with(mRootActivity).load(X3StringUtils.getImageUrl(url)).into(iv_image);
    }

    void initView() {
        btn_yes = findViewById(R.id.btn_yes);
        iv_image = findViewById(R.id.iv_image);
        iv_bg = findViewById(R.id.iv_bg);

        btn_yes.setOnClickListener(view -> dismiss());
    }

    @SuppressWarnings("deprecation")
    private void initDialogWindow() {
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);

        WindowManager m = mRootActivity.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用

        try {
            d.getSize(mScreenPoint);
        } catch (NoSuchMethodError ignore) {
            mScreenPoint.x = d.getWidth();
            mScreenPoint.y = d.getHeight();
        }

        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (mScreenPoint.x * 0.80);    // 宽度设置为屏幕的0.9
        dialogWindow.setAttributes(p);
    }
    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("拍照页面图片参考弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("拍照页面图片参考弹窗");
    }
}

